package com.cooper.dao.core;

import java.util.List;

import com.cooper.entity.LogUserLogin;
import com.cooper.entity.UserInfo;
import com.cooper.pagination.PageInfo;
import com.cooper.vo.UserSearchVo;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.Insert;
import net.paoding.rose.jade.annotation.ReturnGeneratedKeys;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.Update;

@DAO
public interface UserDAO {
	
	@SQL("select * from sys_user_info where deleted='n' and id=:1")
	UserInfo loadById(Long userId);
	
	@SQL("select * from sys_user_info where deleted='n' and user_name=:1 ")
	UserInfo getUser(String userName);
	
	@SQL("select * from sys_user_info where deleted='n' and status='normal' and user_name=:1.userName and user_password=:1.userPassword ")
	UserInfo checkPassword(UserInfo user);
	
	@ReturnGeneratedKeys
	@Insert(tableName="sys_user_info")
	Long addUser(UserInfo user);
	
	@Update(tableName="sys_user_info", keyColumn="userName")
	int updateUser(UserInfo userInfo);
	
	@SQL("update sys_user_info set user_file_id=:2 where id=:1 ")
	boolean updateUserFileId(Long userId, String userFileId) throws Exception;
	
	@SQL("select count(*) from sys_user_info where deleted='n' and user_name=:1")
	Integer existsUserName(String userName);
	
	@SQL("select * from sys_user_info where deleted='n' and email=:1")
	UserInfo existsUserEmail(String email);

	@SQL("update sys_user_info set group_id=:2 where deleted='n' and id=:1")
	void updateGroupId(Long userId, Long groupId);
	
	@SQL("update sys_user_info set email=:2 where deleted='n' and id=:1 ")
	void updateEmail(Long userId, String email);

	@SQL("update sys_user_info set modifier=:1.modifier,gmt_modified=:1.gmtModified,user_password=:1.userPassword where deleted='n' and user_name=:1.userName ")
	boolean changePassword(UserInfo user);
	
	@SQL("update sys_user_info set modifier=:1.modifier,gmt_modified=:1.gmtModified,gmt_last_login=:1.gmtModified where deleted='n' and user_name=:1.userName ")
	int updateGmtLastLogin(LogUserLogin loginLog);
	
	@SQL("update sys_user_info set modifier=:1.modifier,gmt_modified=:1.gmtModified,status='invalid' where deleted='n' and user_name=:1.userName ")
	int invalidUser(UserInfo userInfo);
	
	@SQL("update sys_user_info set modifier=:1.modifier,gmt_modified=:1.gmtModified,status='normal' where deleted='n' and user_name=:1.userName ")
	int restoreUser(UserInfo userInfo);
	
	@SQL("select count(*) from sys_user_info where deleted='n' "
			+ " #if(0 < :1.userId){ and user_id=:1.userId} "
			+ " #if(null !=:1.userName && '' != :1.userName){ and user_name=:1.userName} ")
	int queryUserInfoCount(UserSearchVo searchVo);
	
	@SQL("select * from sys_user_info where deleted='n' "
			+ " #if(0 < :1.userId){ and user_id=:1.userId} "
			+ " #if(null !=:1.userName && '' != :1.userName){ and user_name=:1.userName} "
			+ "  limit :2.startIndex,:2.pageSize ")
	List<UserInfo> queryUserInfoList(UserSearchVo searchVo, PageInfo pageInfo);
}